package com.heima.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.reggie.common.deleteException;
import com.heima.reggie.mapper.SetmealMapper;
import com.heima.reggie.pojo.Setmeal;
import com.heima.reggie.pojo.SetmealDish;
import com.heima.reggie.pojo.SetmealDto;
import com.heima.reggie.service.SetmealDisheService;
import com.heima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDisheService setmealDishesService;

    @Override
    public void saveDish(SetmealDto setmealDto) {
     this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<SetmealDish> collect = setmealDishes.stream().map((set) -> {
            Long id = setmealDto.getId();

            set.setSetmealId(id);
            return set;

        }).collect(Collectors.toList());

        setmealDishesService.saveBatch(collect);

    }

    @Override
    public void deleteByID(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> lam = new LambdaQueryWrapper<>();

        lam.in(Setmeal::getId,ids);
        lam.eq(Setmeal::getStatus,1);
        int count = this.count(lam);
        if (count>0) {
       throw  new  deleteException("当前套餐为在售状态");

        }
        LambdaQueryWrapper<SetmealDish> setlam = new LambdaQueryWrapper<>();
        setlam.in(SetmealDish::getSetmealId,ids);
        setmealDishesService.remove(setlam);
        this.removeByIds(ids);


    }

    @Override
    public void status(List<Long> ids, int status) {
//        LambdaQueryWrapper<Setmeal> set = new LambdaQueryWrapper<>();
//        set.in(Setmeal::getId,ids);
//        List<Setmeal> list = this.list(set);
//        List<Setmeal> collect = list.stream().map((lis) -> {
//            lis.setStatus(status);
//            return lis;
//        }).collect(Collectors.toList());


        List<Setmeal> setmeals = this.listByIds(ids);
        setmeals.stream().map((list)->{
            list.setStatus(status);

            return list;
        }).collect(Collectors.toList());

      this.updateBatchById(setmeals);


    }
}
